<template>
  <div class="table-box">
    <ProTable
      ref="proTableRef"
      row-key="id"
      title="物种变化趋势列表"
      :columns="columns"
      :request-api="loadTable"
      highlight-current-row>
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <ElButton type="primary" :icon="CirclePlus" @click="onAddClicked">新增</ElButton>
      </template>
      <!-- 账户操作 -->
      <template #operation="scope">
        <!-- <ElButton type="primary" link :icon="View" @click="onViewClicked(scope.row)">查看</ElButton> -->
        <ElButton type="warning" link :icon="EditPen" @click="onEditClicked(scope.row)">编辑</ElButton>
        <ElButton type="danger" link :icon="Delete" @click="onDeleteClicked(scope.row)">删除</ElButton>
      </template>
    </ProTable>
    <ChangeTrendForm ref="formRef" @success="refresh" />
  </div>
</template>

<script setup lang="ts" name="ZLChangeTrend">
import { CirclePlus, Delete, EditPen } from '@element-plus/icons-vue'
import ChangeTrendForm from './changeTrend-form.vue'
import ProTable from '@/components/ProTable/index.vue'
import { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { ref, reactive } from 'vue'
import { ChangeTrend } from '@api/tby/summarize/changeTrend/interface'
import { pageChangeTrend, removeChangeTrend } from '@api/tby/summarize/changeTrend'
import { ElMessage, ElMessageBox } from 'element-plus'
import { FormType } from '@/enums'
import { optionsSpeciesCategory } from '@api/tby/common/speciesCategory'

const proTableRef = ref<ProTableInstance>()

function refresh() {
  proTableRef.value?.getTableList()
}

function getIndex(index: number) {
  const { current, size } = proTableRef.value!.pageable
  return (current - 1) * size + index + 1
}

// 表格列
const columns = reactive<ColumnProps<ChangeTrend.Record>[]>([
  { type: 'index', label: '序号', width: 60, index: getIndex },
  {
    prop: 'speciesCode',
    label: '种类',
    enum: typeLevels,
    search: { el: 'select', key: 'speciesCode' },
    align: 'center',
    minWidth: 200
  },
  {
    prop: 'statisticalTime',
    label: '统计时间',
    search: { el: 'date-picker', key: 'statisticalTime', props: { type: 'month' } },
    align: 'center',
    minWidth: 200
  },
  { prop: 'statisticalNum', label: '数量', align: 'center', minWidth: 200 },
  { prop: 'operation', label: '操作', fixed: 'right', width: 230 }
])

const loadTable = (query: ChangeTrend.Query) => {
  query.order = 'statistical_time desc'
  return pageChangeTrend(query)
}

const formRef = ref<InstanceType<typeof ChangeTrendForm> | null>(null)

// 物种分类
async function typeLevels() {
  const data = await optionsSpeciesCategory()
  return data
}

// 添加
const onAddClicked = () => {
  formRef.value?.open(FormType.ADD)
}

// 修改
const onEditClicked = (row: any) => {
  formRef.value?.open(FormType.EDIT, row.id)
}

// 删除
const onDeleteClicked = (row: any) => {
  ElMessageBox.confirm('是否删除数据?', '温馨提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    removeChangeTrend(row.id).then(response => {
      console.log(response)
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      proTableRef.value?.getTableList()
    })
  })
}
</script>
